﻿using System;
using System.IO;
using BEGDC.Utility.NHibernate;
using NHibernate;

namespace BEGDC.PreContract.BLL
{
    public class SnapshotOperation : ISnapshotOperation
    {
        private ISessionFactory mFactory;

        public SnapshotOperation(ISessionFactory factory)
        {
            mFactory = factory;
        }

        public int CreateSnapshot(string snapshotName, string logicalFileName, string fileStreamPath)
        {
            const string StoreProcedureSPCreateSnapshot = "SPCreateSnapshot";
            const string ParameterNameSnapshotName = "snapshotName";
            const string ParameterNameLogicalFileName = "logicalFileName";
            const string ParameterNameFileStreamPath = "fileStreamPath";

            using (ISession session = mFactory.OpenSession())
            {
                return DAO.ExecuteNonQuery(session, StoreProcedureSPCreateSnapshot, new Tuple<string, object>[] {
                    new Tuple<string, object>(ParameterNameSnapshotName, snapshotName),
                    new Tuple<string, object>(ParameterNameLogicalFileName, logicalFileName),
                    new Tuple<string, object>(ParameterNameFileStreamPath, fileStreamPath)});
            }
        }
        
        public DateTime GetSnapshotCreationTime(string snapshotName)
        {
            string QueryString = string.Format("SELECT a.[create_date] FROM [master].[sys].[databases] AS a WHERE a.[name]='{0}'", snapshotName);
            object retValue = null;
            using (ISession session = mFactory.OpenSession())
            {
                retValue = DAO.ExecuteScalar(session, QueryString);
            }
            DateTime dateTime = DateTime.MinValue;
            if (retValue != null)
            {
                DateTime.TryParse(retValue.ToString(), out dateTime);
            }
            return dateTime;
        }

        public string GetSqlServerDefaultDataFilePath()
        {
            string QueryString = "select a.[physical_name] from [sys].[database_files] AS a WHERE a.[type]=0";
            object retValue = null;
            using (ISession session = mFactory.OpenSession())
            {
                retValue = DAO.ExecuteScalar(session, QueryString);
            }
            if (retValue != null)
            {
                string fullPath = retValue.ToString();
                FileInfo fileInfo = new FileInfo(fullPath);
                return fileInfo.DirectoryName;
            }
            else
            {
                return string.Empty;
            }
        }

        public string GetLogicalFileName()
        {
            string QueryString = "select a.[name] from [sys].[database_files] AS a WHERE a.[type]=0";
            object retValue = null;
            using (ISession session = mFactory.OpenSession())
            {
                retValue = DAO.ExecuteScalar(session, QueryString);
            }
            return (retValue == null)? string.Empty : retValue.ToString();
        }
    }
}
